iT邦幫忙

2024 iThome 鐵人賽

DAY 6
0

/images/emoticon/emoticon02.gif
卡bug 明天繼續

import { Component } from '@angular/core';
import { LinePayComponent } from './line-pay/line-pay.component';
import { environment } from '../../../environment';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-day-6-page',
  standalone: true,
  imports: [LinePayComponent],
  templateUrl: './day-6-page.component.html',
  styleUrl: './day-6-page.component.scss'
})
export class Day6PageComponent {
  private apiUrl = environment.linePayApiUrl;

  constructor(private http: HttpClient) {}

  // 發送付款請求
  requestPayment(data: any): Observable<any> {
    const headers = new HttpHeaders({
      'Content-Type': 'application/json',
      'X-LINE-ChannelId': environment.linePayChannelId,
      'X-LINE-Authorization': environment.linePayChannelSecret,
    });

    return this.http.post(`${this.apiUrl}/v3/payments/request`, data, { headers });
  }

  ngOnInit(): void {
    this.requestPayment(paymentRequest).subscribe(response => {
      if (response.info.paymentUrl.web) {
        // 重定向至 LINE Pay 網頁支付頁面
        window.location.href = response.info.paymentUrl.web;
      }
    });

  }
}


const paymentRequest = {
  amount: 100, // 金額
  currency: 'TWD', // 幣別
  orderId: 'ORDER_ID',
  packages: [
    {
      id: 'PACKAGE_ID',
      amount: 100,
      name: 'Product Name',
      products: [
        {
          name: 'Product 1',
          quantity: 1,
          price: 100,
        },
      ],
    },
  ],
  // redirectUrls: {
  //   confirmUrl: 'localhost://5300/confirm',
  //   cancelUrl: 'localhost://5300/cancel',
  // },
};



上一篇
螢幕寬度的廣播
下一篇
處理一個動態元件模板
系列文
Angular 元件煉成練習計畫12
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言